// pages/biz/enterAuction/enterAuction.js
//引入请求接口文件
import Serv from './enterAuctionServ';
var regeneratorRuntime = require("../../../../lib/js/runtime-module.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sysPrice:0,//减价不得低于价格
    current:0,//当前输入框的价格
    addPrice:0,//加价幅度
    myPrice:0,//我的竞拍价
    check:0,//判断是否勾选
    checked: false,
    first:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.id)
    //todo options.id
    this.queryCurrentMaxPrice({ seckillProductId: options.id})
    this.setData({
      seckillProductId: options.id
    })
  },
  //竞拍最高价
  async queryCurrentMaxPrice(parmas, cbk) {
    let { data } = await Serv.queryCurrentMaxPrice(parmas)
    let myPrice = data.myPrice ? data.myPrice : 0
    this.setData({
      current: Number(data.initPrice),
      myPrice: Number(myPrice),
      sysPrice: Number(data.initPrice),
      addPrice: Number(data.grads)
    })
    cbk && cbk(data)
  },
  toDecimal(x) {
  var val = Number(x) 
  if(!isNaN(parseFloat(val))) {
    val = val.toFixed(2);
  }
  return val;   
  },

  //发送竞拍
  async submitAuction(parmas, cbk) {
    let  data = await Serv.submitAuction(parmas)
    cbk && cbk(data)
  },
  remove:function(e){
    let { addPrice, myPrice, current, sysPrice } = this.data
    if (current!=sysPrice){
      current = Number(current)
      current -= Number(addPrice)
      this.setData({
        current: this.toDecimal(current),
      })
    }else{
      //todo 弹出弹窗
    }
  },
  add:function(e){
    let { addPrice, myPrice, current, first } = this.data
    console.log(current, addPrice)
    current = Number(current)
    current += Number(addPrice)
    this.setData({
      current: this.toDecimal(current)
    })
    
  },
  listenCheckboxChange: function (e) {
    console.log('当checkbox-group中的checkbox选中或者取消是我被调用');
    let {check} = this.data 
    if (e.detail.value.length==0) {
      check = 0
    }
    else {
      check = 1
    }
    this.setData({
      check
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  btnConfim:function(){
    let { addPrice, sysPrice, current, check, myPrice,seckillProductId } = this.data
    if(check==0){
      wx.showToast({ title: '请阅读并确认叫价协议', icon: "none" })
    }else{
      let parmas = {
        seckillProductId: seckillProductId,
        accountId:123,
        price: current
      }
      this.submitAuction(parmas,(data)=>{
        if(data.success){
          wx.navigateTo({
            url: `../autionSuccess/autionSuccess?price=${data.data.maxPrice}`
          })
        }else{
          //todo 失败弹出框
          let { seckillProductId} = this.data
          wx.showToast({
            title: data.msg,
            icon:'none'
          })
          this.queryCurrentMaxPrice({ seckillProductId})
        }
      })
      
    }
  },
  onReady: function () {
    let Notfirst = false //todo ajax 是否第一次进入
    if(Notfirst){
      this.setData({
        checked: true
      })
    }
    
  },

})